<?php
class Admin_product_category extends Admin_Controller
{
	public $menu_id = 11;
	public $budgetkey = array("MainCategory","SecondaryCategory","ThirdCategory");
	public $icon = array("MainCategory"=>"budget_plan.png","SecondaryCategory"=>"budget_type.png","ThirdCategory"=>"plan_ico.png");
	public function __construct()
	{
		parent::__construct();
		$this->load->model('product/product_category_model','pcategory');		
	}
	
	public function index()
	{
		$menu_id=11;
		$data['menu_id'] = $menu_id;
		$menu_name = GetMenuProperty($menu_id,'title');
		$data['urlpage']="admin_product_category";
		if(is_login()){
			if(permission($menu_id, 'canview')=='')redirect('admin');
			$condition = "1=1";
			//$condition .= @$_GET['type']!='' ? " AND page='".$_GET['type']."'" : "";
			$data['dataList'] = $this->pcategory->get();
			$data['pagination'] = $this->pcategory->pagination();
			$this->template->build('index',$data);
		}
		else{
			
			redirect('admin');	
		}
	}
	public function form($id=FALSE){
		$menu_id=$this->menu_id;
		$curr_menu = $this->db->getone("SELECT title FROM admin_menu where id=".$menu_id);					
		$data['id'] = $id;
		$data['rs'] = @$this->pcategory->get_row($id);
		if($id>0){
		$action='View';
		$description = $action.' '.$curr_menu.' : '.$data['rs']['name'];		
		save_log($menu_id,$action,$description);			
		}
		$this->template->build('form',$data);
	}
	public function SecondaryCategory($pid=FALSE,$id=FALSE)
	{
		$menu_id=11;
		$menu_name = GetMenuProperty($menu_id,'title');
		if(permission($menu_id, 'canview')=='')redirect('admin');
		$data['pid'] = $pid;
		$data['id'] = $id;
		$data['main_category'] = $this->pcategory->where("pid=0")->get(FALSE,TRUE);
		$data['rs'] = @$this->pcategory->get_row($id);
		if($id>0)
			{
				$action='View';
				$description = $action.' '.$menu_name.' : '.$data['rs']['name'];		
				save_log($menu_id,$action,$description);
			}	
		$this->template->build('secondary_form',$data);
	}
	public function ThirdCategory($pid=FALSE,$id=FALSE)
	{
		//$this->db->debug=true;
		$menu_id=11;
		$menu_name = GetMenuProperty($menu_id,'title');
		if(permission($menu_id, 'canview')=='')redirect('admin');
		$data['pid'] = $pid;
		$data['id'] = $id;
		$data['main_category'] = $this->pcategory->where("pid=0")->get(FALSE,TRUE);
		$data['c_main_category'] = $this->db->getone("SELECT pid FROM product_category WHERE ID=".$pid);
		$data['secondary_category'] = $this->pcategory->where("pid=".$data['c_main_category'])->get(FALSE,TRUE);		
		$data['rs'] = @$this->pcategory->get_row($id);
		if($id>0)
			{
				$action='View';
				$description = $action.' '.$menu_name.' : '.$data['rs']['name_th'];		
				save_log($menu_id,$action,$description);
			}	
		$this->template->build('third_form',$data);
	}
	function get_data_tree($step,$pid)
	{
		//$this->db->debug=true;	
		$menu_id=11;	
		$keyindex = array_search($step, $this->budgetkey);		
		$dataList = '';		
		$result = $this->pcategory->where("pid=".$pid)->get(FALSE,TRUE);
		$dataList .= $keyindex>0 ? "<ul>" : ''; 
		foreach($result as $row):		
		$dataList .= '<li>';
		$dataList .='<img src="images/tree/'.@$this->icon[$step].'" />'.$row['name_th'];
		if($keyindex < 2)
		
		if(permission($menu_id, 'canadd')!=''){
		$dataList .='<input type="button" class="btn_addico vtip" value="Add '.$this->budgetkey[$keyindex+1].'" title="เพิ่มรายการ" onclick="window.location=\'admin_product_category/'.@$this->budgetkey[($keyindex+1)].'/'.$row['id'].'\'"/>';
		}
		if(permission($menu_id, 'canedit')!=''){
		$dataList .='<input type="button" class="btn_editico vtip" value="Edit This Item"  title="แก้ไขรายการนี้" onclick="window.location=\'admin_product_category/'.@$this->budgetkey[($keyindex)].'/'.$row['pid'].'/'.$row['id'].'\'"/>';
		}
		if(permission($menu_id, 'candelete')!=''){ 
		$dataList .='<input type="button" class="btn_deleteico vtip" value="Delete This Item"   title="ลบรายการนี้" onclick="redirect(\'admin_product_category/delete/'.$row['id'].'\');")" />';
		} 
		if(($keyindex+1)<count($this->budgetkey))
		{
			$dataList .= $this->get_data_tree($this->budgetkey[($keyindex + 1)],$row['id']);
		}
		$dataList .='</li>';	
		endforeach;
		$dataList .= $keyindex>0 ? "</ul>" : '';
		return $dataList;				
	}
	public function select_sub_category($pid=FALSE,$control_name=FALSE,$option=FALSE)
	{
		$pid = isset($_POST['pid'])? $_POST['pid'] : $pid;
		$control_name = isset($_POST['control_name']) ? $_POST['control_name'] : $control_name;
		$result = $this->pcategory->where("pid=".$pid)->get(FALSE,TRUE);
		$select = '<select name="'.$control_name.'"><option value="0">--select secondary category</option>';
		foreach($result as $item):
			$select .= '<option value="'.$item['id'].'">'.$item['name_th'].'</option>';
		endforeach;
		$select.='</select>';
		echo $select;
		
	}
			
	function MainCategory($pid=FALSE,$id=false)
	{
		$menu_id=11;
		$menu_name = GetMenuProperty($menu_id,'title');
		if(permission($menu_id, 'canview')=='')redirect('admin');		
		$data['id']=$id;
		if(is_login()){			
			$data['rs'] = @$this->pcategory->get_row($id);			
			if($id>0)
			{
				$action='View';
				$description = $action.' '.$menu_name.' : '.$data['rs']['name_th'];		
				save_log($menu_id,$action,$description);
			}					
			$this->template->build('form',$data);
		}
		else{
			
			redirect('admin');	
		}
	}
	public function save(){
		//$this->db->debug = true;
		$menu_id=11;	
		$menu_name = GetMenuProperty($menu_id,'title');
		if($_POST['id']!='')
		{
			if(permission($menu_id, 'canedit')=='')redirect('admin_product_category');
			$action='Update';
			$description = $action.' '.$menu_name.' : '.$_POST['name'];		
			save_log($menu_id,$action,$description);
		}else{
			if(permission($menu_id, 'canadd')=='')redirect('admin_product_category');	
			$action='Add';
			$description = $action.' '.$menu_name.' : '.$_POST['name'];		
			save_log($menu_id,$action,$description);
		}	
		$id = $this->pcategory->save($_POST);		
		//set_notify('success', lang('save_data_complete'));
		redirect('admin_product_category');
	} 
	function delete($id=FALSE){
		$menu_id=11;
		$menu_name = GetMenuProperty($menu_id,'title');
		if(permission($menu_id, 'candelete')=='')redirect('admin_product_category');
		if($id){
			$data = $this->pcategory->get_row($id);
			$action='Delete';
			$description = $action.' '.$menu_name.' : '.$data['name'];		
			save_log($menu_id,$action,$description);
			$this->pcategory->delete($id);
			//set_notify('success', lang('delete_data_complete'));
		}
		redirect('admin_product_category');
	}
}
?>